package com.notetool.util.tokenparser;

import com.notetool.util.tokenparser.semantic.ParserSemanticException;
import com.notetool.util.tokenparser.syntax.Model;

public abstract class Token 
{
    
    ////////////////////
    // Members
	private Model model;
    private Token previous;
    private Token next;

    ////////////////////
    // Constructors
    public Token (Model mdl)
    {
    	// Store data
        model = mdl;
        
        // Chain to nothing
        previous = null;
        next = null;
    }
    
    ////////////////////
    // Abstract methods
    public abstract String toString ();
    
    ////////////////////
    // Private methods
    private void setPrevious (Token prv)
    {
        previous = prv;
    }
    
    private void setNext (Token nxt)
    {
        next = nxt;
    }
    
    ////////////////////
    // Public methods
    public Token getPrevious ()
    {
        return previous;
    }
    
    public Token getNext ()
    {
        return next;
    }
    
    public Model getModel ()
    {
        return model;
    }
    
    public Token link (Token token)
    {
        if (token == null)
            return null;
        
        // Adjust the new token's next value. If the that value is a token (!= null) then we adjust the previous value of that token
        token.setNext (next);
        if (next != null)
            next.setPrevious(token);
        
        // Chain token after this one
        next = token;
        token.setPrevious (this);
        
        // Return the token we just added
        return token;
    }
    
    public void unLink ()
    {
        // Adjust previous token
        if (previous != null)
            previous.setNext (next);
        
        // Adjust next
        if (next != null)
            next.setPrevious (previous);
        
        // Clear references
        next = null;
        previous = null;
    }
    
    public void validateSemantics () throws ParserSemanticException
    {
        if (model == null)
            return;
        
        model.validateSemantics (this);
    }
    
    public boolean is (String cls)
    {
        if (cls == null)
            return false;
        
        return model.is (cls);
    }
    
    public String[] getClasses ()
    {
        return model.classesArray ();
    }
    
}
